package com.hailong.zyt.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hailong.zyt.beans.Major;
import com.hailong.zyt.beans.Student;
import com.hailong.zyt.service.MajorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author hailong
 * @since 2021-06-08
 */
@Controller
public class MajorController {

    private MajorService majorService;

    /**
     * 来到专业管理界面
     * */
    @GetMapping("/admin/majors")
    public String majors(@RequestParam(value = "pn", defaultValue = "1") Integer pn, Model model) {
        Page<Major> majorPage = new Page<>(pn, 8);
        IPage<Major> page = majorService.page(majorPage);
        model.addAttribute("majors", page);
        return "/admin/admMajor";
    }

    /**
     * 添加专业
     * */
    @PostMapping("/admin/addMajor")
    public String addMajor(Major major) {
        if(major != null && major.getMjName() != null){
            majorService.saveOrUpdate(major);
        }
        return "redirect:/admin/majors";
    }

    @Autowired
    public void setMajorService(MajorService majorService) {
        this.majorService = majorService;
    }
}

